package demo8;

public class Test {

    public int longestOnes(int[] nums, int k) {
        int n = nums.length, zero = 0, ret = 0;
        for (int left = 0, right = 0 ; right < n; right++) {
            if (nums[right] == 0) zero++; //入窗口
            while (zero > k) {  //判断
                if (nums[left++] == 0) zero--;  //出窗口
            }
            ret =  Math.max(ret, right - left + 1); //更新结果
        }
        return ret;
    }

    public int minOperations(int[] nums, int x) {
        int sum = 0, n = nums.length;
        for (int a : nums) {
            sum += a;
        }
        int target = sum - x, tmp = 0, ret = -1;
        if (target < 0) return -1;
        for (int left = 0, right = 0; right < n; right++) {
            tmp += nums[right];
            while (tmp > target) {
                tmp -= nums[left++];
            }
            if (tmp == target) {
                ret = Math.max(ret, right - left + 1);
            }
        }
        if (ret == -1) return ret;
        else return n - ret;
    }

}
